Master the Logic of "If, Then, Else" to Build Smart, Responsive Programs.

if, elif, else)In the previous chapter, we looked at Operators—the tools we use to compare values. Now, we are going to use those tools to give our programs "brains."
In the real world, we make decisions constantly: If it is raining, I will take an umbrella. Else, I will wear sunglasses. Programming works exactly the same way using Control Flow.
if Statement: The Basic ConditionThe if statement tells Python to run a block of code only if a specific condition is True.
Syntax Example:
age = 20
if age >= 18:
print("You are eligible to vote!")
Note the Colon (:): This tells Python that a block of code is starting.
Note the Indentation: The print line is shifted to the right. This tells Python exactly which code "belongs" to the if statement.
else Statement: The AlternativeWhat if the condition is False? The else statement handles the "otherwise" scenario.
Syntax Example:
balance = 450
course_fee = 500
if balance >= course_fee:
print("Enrollment successful!")
else:
print("Insufficient funds. Please add money to your wallet.")
elif Statement: Multiple ChoicesSometimes life isn't just "Yes" or "No." When you have multiple conditions to check, we use elif (short for "else if"). Python checks these in order and stops at the first one that is True.
Practical Example: Grading System
marks = 85
if marks >= 90:
print("Grade: A+")
elif marks >= 80:
print("Grade: A")
elif marks >= 70:
print("Grade: B")
else:
print("Keep practicing! You can do better.")
Remember the Logical Operators (and, or) from our chart? We use them to check two things at once.
and: Both conditions must be True.
or: At least one condition must be True.
Example:
has_laptop = True
has_internet = True
if has_laptop and has_internet:
print("You are ready to start the Python course!")
Imagine you are building a tool for TeachLive.in to calculate a student's project bonus.
Your Task: Write a program where:
If a student completes more than 10 projects, they get a "Gold" badge.
If they complete between 5 and 10, they get a "Silver" badge.
Otherwise, they get a "Bronze" badge.
Move beyond linear coding and discover how to make your Python programs interactive. This guide breaks down Conditional Statements (if, elif, and else) using real-world logic. You will learn how to use comparison operators to create decision-making branches, allowing your applications to react differently based on user input—the first step toward building truly "intelligent" software.
If-else In Python Conditional Loop In Python Gagan Khanna 9312352488